package PenguinsFarm
{
	import Interfaces.Penguin.MIPenguinObj;
	import Interfaces.PenguinsFarm.MIPenguinsFarmController;
	
	import PenguinsFarm.Penguins.PenguinsModel;
	import PenguinsFarm.Penguins.PenguinsEntity;
	import PenguinsFarm.Penguins.PenguinsRenderer;
	import PenguinsFarm.Renderer.CageBackground;
	
	import XMFramework.Interface.Architechture.SMController;
	
	public class PenguinsFarmController extends SMController implements MIPenguinsFarmController
	{
		public var penguinsList:Vector.<PenguinsEntity> = new Vector.<PenguinsEntity>();
		
		public function PenguinsFarmController()
		{
			super();
		}
		
		public function addPenguins( target:MIPenguinObj ):void
		{
			var pengObj:PenguinsEntity = target as PenguinsEntity;
			
			coreSNSFramework.runtimeUpdater.addFunction( pengObj.Renderer.onFrameUpdate );
			
			var farmView:PenguinsFarmView = (Global.CoreSNSFramework.getArchView( ShareMacros.MODULE_PENGUINS_FARM) as PenguinsFarmView);
			farmView.getCageBackground().getPenguinsContainer().addChild( pengObj.Renderer );
			pengObj.Renderer.startRendering();
			
			penguinsList.push(target);
			(viewLayer as PenguinsFarmView).penguinsRenderList.push( pengObj.Renderer );
		}		
		
		public function removePenguins( target:MIPenguinObj ):void
		{
			var pengObj:PenguinsEntity = target as PenguinsEntity;
			
			coreSNSFramework.runtimeUpdater.removeFunction( pengObj.Renderer.onFrameUpdate );
			
			var farmView:PenguinsFarmView = (Global.CoreSNSFramework.getArchView( ShareMacros.MODULE_PENGUINS_FARM) as PenguinsFarmView);
			if( pengObj.Renderer.parent === farmView.getCageBackground().getPenguinsContainer() )
				farmView.getCageBackground().getPenguinsContainer().removeChild( pengObj.Renderer );
			
			penguinsList.splice(penguinsList.indexOf(target), 1);
		}
		
		public function removeAllPenguins() : void
		{
			while (penguinsList.length != 0) 
			{
				this.removePenguins(penguinsList[0]);
			}			
		}
		
		//=======================================================
		public function createPenguin( type:String ):MIPenguinObj
		{
			var obj:PenguinsEntity	=	new PenguinsEntity();
			
			switch( type )
			{
				default:
					obj.Renderer	=	new PenguinsRenderer( obj );
					obj.Model		=	new PenguinsModel( obj );
					break;
			}
			
			return obj;	
		}
		
		// Xóa penguins khỏi danh sách Penguins, Peculiar Data & gửi thông báo lên server
		public function destroyPenguin( target:PenguinsEntity ):void
		{
			/*// Lấy ra index của penguins trong chuồng
			var index:int = Global.penguinsCageScene.penguinsList.indexOf( target );
			// Hủy penguins khỏi chuồng
			Global.penguinsCageScene.penguinsList.splice( index, 1 );
			Global.penguinsCageScene.cageBackground.penguinsContainer.removeChild( target.rendererObj );
			// Hủy penguins data
			PeculiarData.singleton.removeResource( target.logicalObj.TypeId, target.logicalObj.PenguinsId, target );
			// Gọi serverfunc để hủy penguins
			ServerFunc.onDestroyPenguins( target.logicalObj.PenguinsId );*/
		}
		
	}
}